<template>
	<view class="container">
		<!-- #ifdef H5 -->
		<uni-header></uni-header>
		<!-- #endif -->

		<view class="add-box com-box">
			<view class="list" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)" @longpress="deleteShow(item)">
				<view class="wrapper">
					<view class="u-box">
						<text class="name">{{item.name}}</text>
						<text class="mobile">{{item.notes}}</text>
					</view>
					<view class="address-box">
						<text v-if="item.isDefault>0" class="tag">默认</text>
						<text class="address">{{item.provinceName}} {{item.cityName}} {{item.areaName}}</text>
					</view>
					<view class="address-box">
						<text class="address">{{item.address}}</text>
					</view>
				</view>
				<text class="icon icon-edit" @click.stop="addAddress('edit', item)"></text>
			</view>
			<button type="primary" class="add-btn" @click="addAddress('add')">新增地址</button>
		</view>

		<!-- #ifdef H5 -->
		<uni-footer></uni-footer>
		<!-- #endif -->
	</view>
</template>

<script>
	import uniHeader from '@/components/h5/uni-header.vue'
	import uniFooter from '@/components/h5/uni-footer.vue'
	export default {
		components: {
			uniHeader,
			uniFooter
		},
		data() {
			return {
				source: 0,
				addressList: []
			}
		},
		onLoad(option) {
			this.source = option.source
			this.refreshList()
		},
		methods: {
			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().addressData = item
					this.$api.prePage().calcFreightPrice()
					uni.navigateBack()
				} else {
					this.$api.prePage().addressData = item
					uni.navigateBack()
				}
			},
			addAddress(type, item) {
				uni.navigateTo({
					url: `/pages/address/create?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			//添加或修改成功之后回调
			refreshList() {
				this.$api.request('/zraddress/selectAddressList', {}, {
					method: 'GET'
				}).then(res => {
					this.addressList = res.data
				})
			},
			deleteShow(item) {
				uni.showModal({
					title: '删除提示',
					content: '您确定要删除该地址吗?',
					showCancel: true,
					confirmText: '删除',
					success: (e) => {
						if (e.confirm) {
							this.$api.request('/zraddress/delAddress', {
								id: item.id
							}, {
								content: 'application/x-www-form-urlencoded	'
							}).then(res => {
								this.refreshList()
							})
						}
					},
					fail: () => {}
				})
			}
		}
	}
</script>

<style lang='scss'>
	page {
		padding-top: 20upx;
	}

	.container {
		position: relative;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		position: relative;
		border-top: 1upx solid $base-color-light;
		background: #fff;

		&:first-child {
			border-top: 0;
		}
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.address-box {
		margin-top: 10upx;
		display: flex;
		align-items: center;

		.tag {
			font-size: 24upx;
			color: $base-color-page;
			margin-right: 10upx;
			background: #fffafb;
			border: 1upx solid $base-color-page;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}

		.address {
			font-size: 28upx;
			color: $base-color-light;
		}
	}

	.u-box {
		font-size: 30upx;
		color: $base-color-dark;

		.name {
			margin-right: 30upx;
		}
	}

	.icon-edit {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $base-color-dark;
		padding-left: 30upx;
	}

	/* #ifdef H5 */
	@media screen and (min-width: 750px) {

		page {
			padding-top: 0;
		}

		.add-box {
			margin: 20px auto;
		}

		.list {
			padding: 20px 30px;
			border-top: 1px solid $base-color-light;

			&:first-child {
				border-top: 0;
			}
		}

		.address-box {
			margin-top: 10px;

			.tag {
				font-size: 14px;
				margin-right: 10px;
				border: 1px solid $base-color-pc;
				border-radius: 4px;
				padding: 4px 10px;
			}

			.address {
				font-size: 16px;

			}
		}

		.u-box {
			font-size: 16px;

			.name {
				margin-right: 30px;
			}
		}

		.icon-edit {
			height: 55px;
			font-size: 30px;
			padding-left: 30px;
			cursor: pointer;
		}

	}

	/* #endif */
</style>
